package com.click.app.webapp.reportObjects;

import com.click.app.model.LaundryItem;
import com.click.app.model.LaundryRequestItem;
import com.click.app.model.ModelSize;
import com.click.app.service.LaundryItemManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author Joel Chuca Condo
 * @date 05/07/2009
 */
public class LaundryRequestTable {

    private List<String> head;
    private List<BodyRow> body;
    private List<LaundryRequestItem> laundryRequestItems;
    private LaundryItemManager laundryItemManager;
    private Set<ModelSize> modelSizes;
    private boolean correct;
    

    public LaundryRequestTable(List<LaundryRequestItem> laundryRequestItems, LaundryItemManager laundryItemManager, Set<ModelSize> modelSizes) {
        this.laundryRequestItems = laundryRequestItems;
        this.laundryItemManager = laundryItemManager;
        this.modelSizes = modelSizes;
        head = new ArrayList<String>();
        body = new ArrayList<BodyRow>();
        correct = false;
        init();
    }

    public List<BodyRow> getBody() {
        return body;
    }

    public List<String> getHead() {
        return head;
    }

    public boolean isCorrect() {
        return correct;
    }

    private void init() {
        head.add("Editar");
        head.add("T. Lavado");
        head.add("Marca");
        if( modelSizes.isEmpty() ){
            correct = false;
            return;
        }

        List<Integer> totalQuantitiesHor = new ArrayList<Integer>();
        for (ModelSize modelSize : modelSizes) {
            head.add(modelSize.getValueSize());
            totalQuantitiesHor.add(0);
        }
        head.add("Total");
        totalQuantitiesHor.add(0);

        if (laundryRequestItems != null) {
            if (laundryRequestItems.size() > 0) {

                BodyRow row = new BodyRow();
                BodyRow rowTotal = new BodyRow(modelSizes.size() + 4);

                rowTotal.add(0,"---");
                rowTotal.add(1,"---");
                rowTotal.add(2,"---");
                for (LaundryRequestItem laundryRequestItem : laundryRequestItems) {

                    row.add(laundryRequestItem.getId().toString());
                    row.add(laundryRequestItem.getLaundryType().getType());
                    row.add(laundryRequestItem.getTradeMark().getName());

                    List<LaundryItem> laundryItems = laundryItemManager.findByOrderSize(laundryRequestItem.getId());
                    int indexModelSize = 0;
                    for (ModelSize modelSize : modelSizes) {
                        int index = 0;

                        for (int i = 0; i < laundryItems.size(); i++) {
                            if(laundryItems.get(i).getPantDesign().getModelSize().equals(modelSize))
                                break;
                            index++;
                        }
                        if(index != laundryItems.size()){
                            row.add(laundryItems.get(index).getQuantity().toString());
                            totalQuantitiesHor.set(index, totalQuantitiesHor.get(index)+laundryItems.get(index).getQuantity());
                            rowTotal.add(indexModelSize+2, laundryItems.get(index).getQuantity().toString());
                            rowTotal.add(rowTotal.size()-1, laundryItems.get(index).getQuantity().toString());
                        }else{
                            row.add("0");
                            
                        }
                        indexModelSize++;
                    }
                    row.add(laundryRequestItem.getQuantity().toString());
                    int lastPos = modelSizes.size();
                    totalQuantitiesHor.set(lastPos, totalQuantitiesHor.get(lastPos)+laundryRequestItem.getQuantity());
                    body.add(row);
                    row = new BodyRow();
                    
                }

                body.add(rowTotal);
                System.out.println("body size: "+body.size());
                correct = true;

            } else {
                System.out.println("Laundry Request Items is empty");
            }
        } else {
            System.out.println("Laundry Request Items is null");
            correct = false;
        }
    }

}
